package com.ws.main;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.soap.SOAPConstants;

/**
 * Axis 调用 webservice
 * 
 * @author 旷明爱
 * @date 2015年12月28日 下午8:11:36
 * @email mingai.kuang@mljr.com
 */
public class WsClient<T> {
 
	/**
	 * 
	 * 调用webservice. (只支持简单类型的参数.int,String ...)
	 * 
	 * @param namespace
	 *            命名空间
	 * @param wsdlUrl
	 *            wsdl 地址
	 * @param methodName
	 *            方法名
	 * @param retClass
	 *            返回类型
	 * @param paramNames
	 *            参数 key
	 * @param paramValues
	 *            传入的参数值
	 * @return
	 * @throws Exception
	 */
	public T invokeWebservice(String namespace, String wsdlUrl ,String soapAction, String methodName,  Class<?> retClass, String[] paramNames,
			Object[] paramValues) throws Exception {
		
		if (paramNames==null  || paramValues==null || paramNames.length!=paramValues.length) {
			System.err.println("参数key 和value 不能为空.且数量对应!");
			return null ;
		} 
		if (methodName==null || "".equals( methodName.trim())) { 
			System.err.println("调用方法名不能为空:"+methodName);
			return null ;
		}
		if (retClass==null) {
			System.err.println("返回类型不能为空:"+methodName);
			return null ;
		}  
		if (namespace==null) {
			namespace="" ;
		}
		
		T result = null ;
		try {
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setSOAPVersion(SOAPConstants.SOAP11_CONSTANTS);
			call.setTargetEndpointAddress(wsdlUrl);
			 
			call.setOperationName(new QName(namespace, methodName));// WSDL里面描述的接口名称
			for (int i = 0; i < paramNames.length; i++) {
				call.addParameter(new QName(namespace, paramNames[i]),XMLType.XSD_ANYSIMPLETYPE , paramValues[i].getClass(), ParameterMode.IN);// 接口的参数
			} 
			call.setSOAPActionURI(soapAction);
			call.setReturnClass(retClass);
			result =(T)call.invoke(paramValues); // 给方法传递参数，并且调用方法
		} catch (Exception e) {
			System.err.println(e.toString());
			throw new Exception("调用webservice异常:" + e);
		}
		return result;
	}

	public static void main(String[] args) throws Exception {
		WsClient<?>  wsClient = new WsClient<String>();
		String namespace = "http://impl.ws.com"; // 命名空间
		String wsdlUrl = "http://127.0.0.1:9090/springws/services/helloService.wsdl?wsdl";
		String soapAction ="";
		String methodName = "sayHello";
		
		Class<String> retClass = String.class;
		String[] paramNames = new String[] { "name" };
		Object[] paramValues = new Object[1];
		paramValues[0] = "hello. day .";
		Object result = null ;
		
//		result = wsClient.invokeWebservice(namespace, wsdlUrl,soapAction, methodName, retClass, paramNames, paramValues);
//		System.out.println("result is: " + result);
//				
//		wsClient = new WsClient<HelloBean>();
//		Class<HelloBean> retClass2 = HelloBean.class;
//		methodName = "sayHelloBean";
//		result = wsClient.invokeWebservice(namespace, wsdlUrl, soapAction,methodName, retClass2, paramNames, paramValues);
//		System.out.println("result is: " + result);
//
//		Class<HelloBean> retClass3 = HelloBean.class;
//		methodName = "sayHelloBean2";
//		paramNames = new String[] { "id","name" };
//		paramValues = new Object[2];
//		paramValues[0] =7;
//		paramValues[1] = "bean 2" ;
//		result = wsClient.invokeWebservice(namespace, wsdlUrl,soapAction, methodName, retClass3, paramNames, paramValues);
//		System.out.println("result is: " + result);
//		
		namespace="http://dectechsolutions.com/Instinct";
		wsdlUrl ="http://10.8.61.52:1480/Instinct_FraudCheckWebService/InstinctFraudCheck.asmx?wsdl";
		soapAction ="http://dectechsolutions.com/Instinct/InstinctFraudCheck_XMLString";
		methodName="InstinctFraudCheck_XMLString";
		paramNames=new String[]{"inputXMLString"};
		paramValues=new Object[]{"<ApplicationSchema><Application><Organisation>MLI</Organisation><Country_Code>CN</Country_Code><Group_Member_Code></Group_Member_Code><Application_Number>2016012016070154</Application_Number><Application_Date>20/01/2016</Application_Date><Application_Type>LOAN</Application_Type><Amount_Limit>4200</Amount_Limit><Branch>16</Branch><Decision></Decision><Decision_Reason></Decision_Reason><Decision_Date></Decision_Date><User_Field1>3101080001</User_Field1><User_Field2>审核未通过</User_Field2><User_Field3></User_Field3><User_Field4></User_Field4><User_Field5>31.245368</User_Field5><User_Field6>121.452277</User_Field6><User_Field7>中国上海市闸北区天目西路街道天目西路490号</User_Field7><User_Field8></User_Field8><User_Field9></User_Field9><User_Field10></User_Field10><User_Field11></User_Field11><User_Field12>G9250</User_Field12><User_Field13>G9250</User_Field13><User_Field14>5250.00</User_Field14><User_Field15>233</User_Field15><User_Field16>1</User_Field16><User_Field17>意外险</User_Field17><User_Field18>A</User_Field18><Applicant><Id_Number1>142729199006180618</Id_Number1><Id_Number2>1</Id_Number2><Id_Number3>1453276751498</Id_Number3><Surname>吴李义</Surname><Sex>M</Sex><Date_of_Birth>18/6/1990</Date_of_Birth><Home_Address1>上海市</Home_Address1><Home_Address2>上海市</Home_Address2><Home_Address3>宝山区</Home_Address3><Home_Address4>长逸路258号2楼</Home_Address4><Home_Phone_Number></Home_Phone_Number><Mobile_Phone_Number>13052586505</Mobile_Phone_Number><Company_Name>来力桌球俱乐部</Company_Name><Company_Address1>上海市</Company_Address1><Company_Address2>上海市</Company_Address2><Company_Address3>宝山区</Company_Address3><Company_Address4>长逸路258号2楼</Company_Address4><Company_Phone_Number>021-36580900</Company_Phone_Number><User_Field1>未婚</User_Field1><User_Field2></User_Field2><User_Field3>4</User_Field3><User_Field4>朋友</User_Field4><User_Field5>工薪</User_Field5><User_Field6>文化、运动、娱乐、传媒、广告设计</User_Field6><User_Field7></User_Field7><User_Field8>714646463</User_Field8><User_Field10>3</User_Field10><User_Field13>121.49773629035</User_Field13><User_Field14>31.343502819442</User_Field14><User_Field15>121.49773629035</User_Field15><User_Field16>31.343502819442</User_Field16></Applicant><User2><User_Field1></User_Field1><User_Field2></User_Field2><User_Field3>null</User_Field3><User_Field4></User_Field4><User_Field5></User_Field5><User_Field6></User_Field6><User_Field7></User_Field7><User_Field8></User_Field8><User_Field9></User_Field9><User_Field10>null</User_Field10><User_Field11></User_Field11><User_Field12></User_Field12><User_Field13></User_Field13><User_Field14></User_Field14><User_Field15></User_Field15><User_Field16></User_Field16><User_Field30/><User_Field29/><User_Field28/><User_Field27/><User_Field26/><User_Field25/><User_Field24/><User_Field23/><User_Field22/><User_Field21/><User_Field20/><User_Field19/><User_Field18/><User_Field17/><User_Field36>0</User_Field36><User_Field37>0</User_Field37></User2></Application></ApplicationSchema>"} ;
		result =wsClient.invokeWebservice(namespace, wsdlUrl,soapAction, methodName, retClass, paramNames, paramValues);
		System.out.println("result is: " + result);
		

	}

	public void callWs() {
		try {
			String ns = "http://impl.ws.com"; // 命名空间
			String endpoint = "http://127.0.0.1:9090/springws/services/helloService.wsdl?wsdl";
			// 直接引用远程的wsdl文件
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(endpoint);
			call.setOperationName(new QName(ns, "sayHello"));// WSDL里面描述的接口名称
			call.addParameter(new QName(ns, "name"), XMLType.XSD_STRING, ParameterMode.IN);// 接口的参数
			call.setReturnType(XMLType.XSD_STRING);// 设置返回类型
			String temp = "axis测试";
			String result = (String) call.invoke(new Object[] { temp });
			// 给方法传递参数，并且调用方法
			System.out.println("result is: " + result);
		} catch (Exception e) {
			System.err.println(e.toString());
		}

	}
}